﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TCompanyRepository : BaseRepository<TCompany>, ITCompanyRepository
    {
        public TCompanyRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }
        /// <summary>
        /// 添加公司信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool AddTcompany(TCompany obj)
        {
            try
            {
                string code = "Company";
                var aobj = Context.TCompanies.AsNoTracking().Where(x => x.Code.Contains(code)).OrderByDescending(x => x.CreateDate).FirstOrDefault();
                if (aobj != null)
                {
                    string pattern = @"\d+";
                    MatchCollection matches = Regex.Matches(aobj.Code, pattern);
                    string value = matches[matches.Count - 1].Value;
                    int values = Convert.ToInt32(value);
                    values++;
                    obj.Code = code + values.ToString("D4");
                }
                else
                {
                    int i = 1;
                    obj.Code = code + i.ToString("D4");
                }

                var m = Context.TCompanies.AsNoTracking().FirstOrDefault(x => x.Code == obj.Code);
                if (m != null)
                {
                    return false;
                }
                obj.CreateUserId = obj.NowUserId;
                obj.CreateUserName = obj.NowUserRealName;
                obj.CreateDate = DateTime.Now;
                Context.TCompanies.Add(obj);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除公司信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelTcompany(int uid)
        {
            try
            {
                var del = Context.TCompanies.Find(uid);
                if (del != null)
                {
                    Context.TCompanies.Remove(del);
                    return Context.SaveChanges() > 0;
                }
                return false;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据id获取公司信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public TCompany GetTcompanyById(int id)
        {
            try
            {
                return Context.TCompanies.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示公司信息
        /// </summary>
        /// <param name="gname"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TCompany> GetTcompanyList(string gname, int pageIndex, int pageSize, out int totalCount)
        {
            try
            {
                var list = Context.TCompanies.AsQueryable();
                if (!string.IsNullOrEmpty(gname))
                {
                    list = list.Where(c => c.Name.Contains(gname));
                }
                totalCount = list.Count();
                list = list.OrderByDescending(x => x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return list.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<TCompany> GetTcompanyList()
        {
            try
            {
                return Context.TCompanies.AsQueryable().ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改公司信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool UpdateTcompany(TCompany obj)
        {
            try
            {
                var oobj = Context.TOwners.AsNoTracking().FirstOrDefault(x => x.Id != obj.Id && x.Code == obj.Code);

                if (oobj != null)
                {
                    return false;
                }
                obj.CreateUserId = obj.NowUserId;
                obj.CreateUserName = obj.NowUserRealName;
                obj.CreateDate = DateTime.Now;
                Context.TCompanies.Update(obj);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
